package com.ruoyi.receipt.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.domain.TbKnowledgeManager;
import com.ruoyi.receipt.service.ITbExampleSubjectManagerService;
import com.ruoyi.receipt.service.ITbKnowledgeManagerService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbExampleSubjectAnalysis;
import com.ruoyi.receipt.service.ITbExampleSubjectAnalysisService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 例题解析审核Controller
 * 
 * @author ruoyi
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/receipt/exampleSubjectAnalysisAudit")
public class TbExampleSubjectAnalysisAuditController extends BaseController
{
    @Autowired
    private ITbExampleSubjectAnalysisService tbExampleSubjectAnalysisService;

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;

    @Autowired
    private ITbExampleSubjectManagerService tbExampleSubjectManagerService;

    /**
     * 查询例题解析审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        startPage();
        List<TbExampleSubjectAnalysis> list = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisList(tbExampleSubjectAnalysis);
        for (int i=0;i<=list.size()-1;i++){
            TbExampleSubjectAnalysis tbExampleSubjectAnalysis1 = list.get(i);
            TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectAnalysis1.getKnowledgeManagerRelated());
            tbExampleSubjectAnalysis1.setZsName(tbKnowledgeManager.getTitle());
            TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(tbExampleSubjectAnalysis1.getSubjectManagerId());
            tbExampleSubjectAnalysis1.setClusteName(tbExampleSubjectManager.getTitle());
        }
        return getDataTable(list);
    }

    /**
     * 导出例题解析审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:export')")
    @Log(title = "例题解析审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        List<TbExampleSubjectAnalysis> list = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisList(tbExampleSubjectAnalysis);
        ExcelUtil<TbExampleSubjectAnalysis> util = new ExcelUtil<TbExampleSubjectAnalysis>(TbExampleSubjectAnalysis.class);
        return util.exportExcel(list, "exampleSubjectAnalysisAudit");
    }
    /**
     * 获取例题解析审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TbExampleSubjectAnalysis tbExampleSubjectAnalysis = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisById(id);
        //根据id 获取绑定的知识点名字
        TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectAnalysis.getKnowledgeManagerRelated());
        tbExampleSubjectAnalysis.setZsName(tbKnowledgeManager.getTitle());
        //根据id 获取绑定的例题名字
        TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(tbExampleSubjectAnalysis.getSubjectManagerId());
        tbExampleSubjectAnalysis.setClusteName(tbExampleSubjectManager.getTitle());
        return AjaxResult.success(tbExampleSubjectAnalysis);
    }
    /**
     * 新增例题解析审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:add')")
    @Log(title = "例题解析审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        tbExampleSubjectAnalysis.setId(idWorker.nextId());
        tbExampleSubjectAnalysis.setIsDel(0);
        tbExampleSubjectAnalysis.setStatus(1);
        return toAjax(tbExampleSubjectAnalysisService.insertTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }

    /**
     * 修改例题解析审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:edit')")
    @Log(title = "例题解析审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        tbExampleSubjectAnalysis.setStatus(1);
        return toAjax(tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }

    /**
     * 删除例题解析审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:remove')")
    @Log(title = "例题解析审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        //根据id查询实体
        TbExampleSubjectAnalysis tbExampleSubjectAnalysis = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisById(ids);
        tbExampleSubjectAnalysis.setIsDel(1);
        return toAjax(tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }

    /**
     * 审核通过
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:Consent')")
    @PutMapping("/AnalysisConsent")
    public AjaxResult AnalysisConsent(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis){

        return  toAjax(tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }
    /**
     * 审核驳回
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysisAudit:Reject')")
    @PutMapping("/AnalysisReject")
    public AjaxResult AnalysisReject(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis){

        return  toAjax(tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }
    /**
     * 根据知识点名字查询解析
     */
    @GetMapping("/konwlegName/{konwlegName}")
    public List<TbExampleSubjectAnalysis> konwlegName(@PathVariable(value = "konwlegName",required = false) String konwlegName){
        startPage();
        List<TbExampleSubjectAnalysis> tbExampleSubjectAnalyses =new ArrayList<>();
        //模糊查询查出知识点list
        List<TbKnowledgeManager> tbKnowledgeManagers = tbKnowledgeManagerService.selectKmName(konwlegName);
        for (int i=0;i<tbKnowledgeManagers.size();i++){
            TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagers.get(i);
            //根据id查询例题list
            List<TbExampleSubjectAnalysis> selcetanalysis = tbExampleSubjectAnalysisService.selcetanalysis(tbKnowledgeManager.getId());
            tbExampleSubjectAnalyses.addAll(selcetanalysis);
        }
        for (int i=0;i<=tbExampleSubjectAnalyses.size()-1;i++){
            TbExampleSubjectAnalysis tbExampleSubjectAnalysis1 = tbExampleSubjectAnalyses.get(i);
            TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectAnalysis1.getKnowledgeManagerRelated());
            tbExampleSubjectAnalysis1.setZsName(tbKnowledgeManager.getTitle());
            TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(tbExampleSubjectAnalysis1.getSubjectManagerId());
            tbExampleSubjectAnalysis1.setClusteName(tbExampleSubjectManager.getTitle());
        }
        return tbExampleSubjectAnalyses;
    }
    @GetMapping("/selectManageList")
    public  TableDataInfo selectManageList(){
        List<TbExampleSubjectManager> tbExampleSubjectManagers = tbExampleSubjectManagerService.selectTbExampleSubjectManagerstatus(new TbExampleSubjectManager());
        return getDataTable(tbExampleSubjectManagers);
    }
}
